package org.onepiece.admin.common.mianshi;

/**
 * Created by IntelliJ IDEA.
 *
 * @author zhujianke@gaodun.com
 * @since 2023/4/14 13:32
 */
public class TreeSort {

    static class TreeNode {
        public TreeNode left, right;
        public int val;

        public TreeNode(int val) {
            this.val = val;
        }
    }

    private static TreeNode build() {
        TreeNode treeNode1 = new TreeNode(1);
        TreeNode treeNode2 = new TreeNode(2);
        TreeNode treeNode3 = new TreeNode(3);
        TreeNode treeNode4 = new TreeNode(4);
        TreeNode treeNode5 = new TreeNode(5);
        TreeNode treeNode6 = new TreeNode(6);
        TreeNode treeNode7 = new TreeNode(7);

        treeNode1.left = treeNode2;
        treeNode1.right = treeNode3;

        treeNode2.left = treeNode4;
        treeNode2.right = treeNode5;

        treeNode3.left = treeNode6;
        treeNode3.right = treeNode7;


        return treeNode1;
    }

    public static void main(String[] args) {
        TreeNode treeNode = build();

        preTraverse(treeNode);
        System.out.println();

        inTraverse(treeNode);
        System.out.println("");

        postTraverse(treeNode);
        System.out.println();

    }


    // 先序遍历
    public static void preTraverse(TreeNode root) {
        if (root != null) {
            System.out.println(root.val);
            preTraverse(root.left);
            preTraverse(root.right);
        }
    }

    // 中序遍历
    public static void inTraverse(TreeNode root) {
        if (root != null) {
            inTraverse(root.left);
            System.out.println(root.val);
            inTraverse(root.right);
        }
    }

    // 后序遍历
    public static void postTraverse(TreeNode root) {
        if (root != null) {
            postTraverse(root.left);
            postTraverse(root.right);
            System.out.println(root.val);
        }
    }

}


